// pages/submit/index.js
import WxValidate from "../../utils/WxValidate";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    formData:{
      phone: "",
    consignee: "",
    address: ""
    }
  },
  // 提示错误
  showModal(error) {
    wx.showModal({
      content: error.msg,
      showCancel: false,
    })
  },
  //验证函数
  initValidate() {
    const rules = {
      consignee: {
        required: true,
        minlength: 3
      },
      phone: {
        required: true,
        tel: true
      },
      address: {
        required: true
      }
    }
    const messages = {
      consignee: {
        required: '请填写收件人',
        minlength: '请输入正确的收件人信息'
      },
      phone: {
        required: '请填写手机号',
        tel: '请填写正确的手机号'
      },
      address: {
        required: '请填写地址',
      }
    }
    this.WxValidate = new WxValidate(rules, messages)
  },
  formSubmit(e) {
    let params = e.detail.value
    console.log(params)
    // 表单校验
    if (!this.WxValidate.checkForm(params)) {
      const error = this.WxValidate.errorList[0]
      this.showModal(error)
      return false
    }
    let that = this
    wx.showModal({
      title: '领取提示',
      content: '确认提交信息',
      success(res) {
        if (res.confirm) {
          console.log(e.detail.value)
          that.setData({
           formData:{
            phone: '',
            address: '',
            consignee: ''
           }

          })
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initValidate();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})